﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Mvc;

using LINQtoSQLDataProvider;

namespace Dragon.Prototype1.Infrastructure.Controls
{
    public class DropDownList:WebControl
    {
        public DropDownList(string id, List<SelectListItem> selectList)
        {
            this.ID = id;
            this.Name = id;
            
            this.SelectList = selectList;
        }

        protected override System.Web.UI.HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Select;
            }
        }
        public List<SelectListItem> SelectList
        {
            get;
            set;
        }

        public override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            foreach (SelectListItem item in SelectList)
            {
                ListItemToOption(item,writer);
            }   
        }
        private void ListItemToOption(SelectListItem item,HtmlTextWriter writer)
        {
            if (item.Value != null) 
            {
                writer.AddAttribute("value",item.Value);
            }
            if(item.Selected)
            {
                writer.AddAttribute("selected","selected");
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Option);
            writer.Write(Microsoft.Security.Application.AntiXss.HtmlEncode(item.Text));
            writer.RenderEndTag();
        }
    }
}
